package com.ruoyi.gp.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.oss.core.OssClient;
import com.ruoyi.oss.entity.UploadResult;
import com.ruoyi.oss.factory.OssFactory;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gp.domain.vo.FamilyEnvironmentVo;
import com.ruoyi.gp.domain.bo.FamilyEnvironmentBo;
import com.ruoyi.gp.service.IFamilyEnvironmentService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【家庭环境】
 *
 * @author ruoyi
 * @date 2024-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/environment")
public class FamilyEnvironmentController extends BaseController {

    private final IFamilyEnvironmentService iFamilyEnvironmentService;

    /**
     * 查询【家庭环境】列表
     */
    @SaCheckPermission("system:environment:list")
    @GetMapping("/list")
    public TableDataInfo<FamilyEnvironmentVo> list(FamilyEnvironmentBo bo, PageQuery pageQuery) {
        return iFamilyEnvironmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出【家庭环境】列表
     */
    @SaCheckPermission("system:environment:export")
    @Log(title = "【导出家庭环境】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FamilyEnvironmentBo bo, HttpServletResponse response) {
        List<FamilyEnvironmentVo> list = iFamilyEnvironmentService.queryList(bo);
        ExcelUtil.exportExcel(list, "【家庭环境】", FamilyEnvironmentVo.class, response);
    }

    /**
     * 获取【家庭环境】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:environment:query")
    @GetMapping("/{id}")
    public R<FamilyEnvironmentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iFamilyEnvironmentService.queryById(id));
    }

    /**
     * 新增【家庭环境】
     */
    @SaCheckPermission("system:environment:add")
    @Log(title = "添加家庭环境",  businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@RequestPart FamilyEnvironmentBo bo, MultipartFile file) {
        Boolean b = iFamilyEnvironmentService.insertByBo(file,bo);
        return toAjax(b);
    }

    /**
     * 修改【家庭环境】
     */
    @SaCheckPermission("system:environment:edit")
    @Log(title = "【修改家庭环境】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FamilyEnvironmentBo bo) {
        return toAjax(iFamilyEnvironmentService.updateByBo(bo));
    }

    /**
     * 删除【家庭环境】
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:environment:remove")
    @Log(title = "【删除家庭环境】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iFamilyEnvironmentService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
